package edu.nf.takeoutdemo.service.consumer;

import edu.nf.takeoutdemo.commons.config.RabbitConfig;
import edu.nf.takeoutdemo.mapper.OrderMapper;
import edu.nf.takeoutdemo.model.entity.Orders;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 *
 * @author zjl
 * @date 2025/10/30
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderServiceConsumer {

    private final OrderMapper orderMapper;

    @RabbitListener(queues = RabbitConfig.DELAY_QUEUE)
    public void receive(String orderNumber){
        Orders orders = orderMapper.getByNumber(orderNumber);
        if (orders.getPayStatus().equals(Orders.UN_PAID)){
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.update(orders);
        }
    }
}
